package com.white.androidstudy.ipc;

import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.net.Uri;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.white.androidstudy.R;
import com.white.androidstudy.aidl.Book;
import com.white.androidstudy.aidl.IBookArrivedListener;
import com.white.androidstudy.aidl.IBookManager;

import java.util.List;

public class ClientActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_client);
        bindService(new Intent(this, BookManagerService.class), sc, Context.BIND_AUTO_CREATE);
    }

    private IBookManager ibk;
    private ServiceConnection sc = new ServiceConnection()
    {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service)
        {
            ibk = IBookManager.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name)
        {
        }
    };

    Lis listener;
    public void click(View view)
    {
        switch (view.getId())
        {
            case R.id.regist:
                if(ibk != null)
                {
                    try
                    {
                        if(listener == null)
                        {
                            listener = new Lis();
                            ibk.addOnBookArrivedListener(listener);
                        }
                    } catch (RemoteException e)
                    {
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.unregist:
                if(ibk != null)
                {
                    try
                    {
                        if(listener != null)
                        {
                            ibk.removeOnBookArrivedListener(listener);
                        }
                    } catch (RemoteException e)
                    {
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.get:
                if(ibk != null)
                {
                    try
                    {
                        List<Book> l = ibk.getBookList();
                        Log.e("s", "l size = " + l.size());
                        Toast.makeText(this, l.size() + "listSize", Toast.LENGTH_SHORT).show();
                    } catch (RemoteException e)
                    {
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.add:
                if(ibk != null)
                {
                    try
                    {
                        ibk.addBook(new Book(3, "天才在左，疯子在右"));
                        List<Book> l = ibk.getBookList();
                        Toast.makeText(this, l.size() + "listSize", Toast.LENGTH_SHORT).show();
                    } catch (RemoteException e)
                    {
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.ipcprovider:
                Uri qurl = Uri.parse("content://com.white.ipcprovider.bookprovider/book");
                ContentResolver p = getContentResolver();
                Cursor cr = p.query(qurl, null,null,null,null);
                if(cr != null)
                {
                    if(cr.moveToNext())
                    {
                        int id = cr.getInt(0);
                        String name = cr.getString(1);
                        float price = cr.getFloat(2);
                        Book b = new Book(id, name);
                        b.price = price;
                        Toast.makeText(this, b.name, Toast.LENGTH_SHORT).show();
                    }
                    cr.close();
                }

                break;
        }
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        unbindService(sc);
    }

    class Lis extends IBookArrivedListener.Stub
    {

        @Override
        public void bookArrived(final Book b) throws RemoteException
        {
            runOnUiThread(new Runnable()
            {
                @Override
                public void run()
                {
                    Toast.makeText(ClientActivity.this, "b = " + b.name, Toast.LENGTH_SHORT).show();
                }
            });

        }
    }
}
